package com.admin.testproject.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.admin.testproject.OnItemClickListener
import com.admin.testproject.R
import com.admin.testproject.Testbean
import com.admin.testproject.activity.NewDetailActivity
import com.admin.testproject.adapter.TestAdapter
import kotlinx.android.synthetic.main.fragment_new_detail.view.*

/**
 * author 吴昶
 * date 2019/6/4
 * 标注
 */
class ListFragment :Fragment(){

    private var fview:View?=null
    private var isTwopage=false
    private val titles= arrayOf("title 001","title 002","title 003","title 004","title 005")
    private val images= arrayOf(R.drawable.test_01,R.drawable.test_02,R.drawable.test_03,R.drawable.test_04,R.drawable.test_05)
    private val data=ArrayList<Testbean>()
    private var adapter:TestAdapter?=null
    private var detailFragment:DetailFragment?=null

    @SuppressLint("InflateParams")
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if(fview==null){
            fview= inflater.inflate(R.layout.fragment_new_detail,null)
            initData()
        }
        val parent=fview?.parent
        if(parent!=null){
            (parent as ViewGroup).removeView(fview)
        }
        return fview
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        isTwopage = activity!!.findViewById<FrameLayout>(R.id.fragment_content)!=null
        if(isTwopage){
            //如果是双屏，加载 DetailFragment
            detailFragment= DetailFragment()
            activity!!.supportFragmentManager.beginTransaction().replace(R.id.fragment_content,detailFragment!!).commit()
        }
        Log.e("****","$isTwopage")
    }


    private fun initData(){
        for(i in 0 until titles.size){
            data.add(Testbean(titles[i],images[i]))
        }
        adapter= TestAdapter(activity!!)
        fview!!.rlv_list.layoutManager=LinearLayoutManager(activity!!)
        fview!!.rlv_list.adapter=adapter
        adapter?.upData(data)
        adapter?.onItemClick= object : OnItemClickListener<Testbean> {
            override fun onItemClick(item: Testbean) {
                if(isTwopage){
                    //双屏直接更新数据
                    click(item)
                }else{
                    //单屏直接跳转到详情activity
                    val intent=Intent(activity!!,NewDetailActivity::class.java)
                    val bundle=Bundle()
                    bundle.putSerializable("bean",item)
                    intent.putExtras(bundle)
                    startActivity(intent)
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        if(isTwopage){
            //双屏初始化详情页数据
            click(data[0])
        }
    }

    fun click(item: Testbean){
        detailFragment?.setValueData(item)
    }

}